package com.stu.manager.personnel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.personnel.entity.SysStudent;
import com.stu.manager.personnel.entity.SysTeacher;
import com.stu.manager.personnel.entity.dto.StudentDto;
import com.stu.manager.personnel.entity.dto.TeacherDto;
import com.stu.manager.personnel.service.SysStudentServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/api/student")
@CrossOrigin
public class SysStudentController {

    @Autowired
    private SysStudentServiceImpl sysStudentService;
    @Autowired
    private LoginHandler loginHandler;
    @PostMapping("pageStudent/{current}/{limit}")
    @ApiOperation("模糊查询加分页展示学生")
    public Result pageStudent(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody(required = false) StudentDto studentDto, HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Page<SysStudent> pageStudent = new Page<>(current, limit);
        QueryWrapper<SysStudent> wrapper = new QueryWrapper<>();
        String stuName = studentDto.getStuName();
        String departmentName = studentDto.getDepartmentName();
        String professionalName = studentDto.getProfessionalName();

        if (!StringUtils.isEmpty(stuName)) {
            wrapper.like("stu_name", stuName);
        }
        if (!StringUtils.isEmpty(departmentName)) {
            wrapper.like("department_name",departmentName);
        }
        if (!StringUtils.isEmpty(professionalName)) {
            wrapper.like("professional_name",professionalName);
        }

        IPage<SysStudent> sysStudentIPage = sysStudentService.pageStudent(pageStudent, wrapper);
        List<SysStudent> records = sysStudentIPage.getRecords();
        long total = sysStudentIPage.getTotal();

        return Result.ok().data("total", total).data("records", records);
    }


    @PostMapping("addStudent")
    @ApiOperation("添加学生")
    public Result addStudent(@RequestBody SysStudent sysStudent,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = sysStudentService.addStudent(sysStudent);
        return result;
    }


    @GetMapping("getStudentById/{id}")
    @ApiOperation("查询学生信息，修改回显")
    public Result getStudentById(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        SysStudent studentById = sysStudentService.getStudentById(id);
        return Result.ok().data("item", studentById);
    }

    @PostMapping("updateStudent")
    @ApiOperation("修改学生信息")
    public Result updateStudent(@RequestBody SysStudent sysStudent,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = sysStudentService.updateStudent(sysStudent);
        return result;
    }

    @DeleteMapping("delStudent/{id}")
    @ApiOperation("删除学生信息")
    public Result delStudent(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        QueryWrapper<SysStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        boolean remove = sysStudentService.remove(wrapper);
        if (remove) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }
    }

}

